home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / source / c_plasma / plasma.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-01-20  |  7.2 KB  |  314 lines

  1. /*
  2.     Plasma by Jan M¢ller & Erik Hansen.
  3.  
  4.        It was compiled in Borland c++, in ANSI mode, using the HUGE memory model.
  5.     Feel free to use it as you desire, you may even name it after your
  6.     grandmother. We don't care.
  7.        The reason why we made it? Well... First of all we wanted to make some
  8.     plasma, just for fun. But when we had finished it turned out to be much
  9.     faster than the plasma we have seen in intros, demos etc...
  10.     Normally the color-cells are 2x4 4x4 or even larger, the 2x4 cell-plasma
  11.     was awfully slow, bot ours ain't, even though it is 2x2 cells.
  12.        Well how can that be???                        ^^^^^^^^^
  13.     Our secret lies in the plasma-calculation!
  14.     (I assume you have guessed that    part already)
  15.     We simply calculate as much as possible before we start showing the goddies.
  16.     The table 'Tab1' is a simple table (320x200 yields 64k) with the distance
  17.     from (x,y) to the center (rounded off to char by simple overflow). The second
  18.     table 'Tab2' is similar to 'Tab1', except we molested it with sine.
  19.     In the mainloop we calculate a body (160x100) by accessing the two tables
  20.     with different pairs of (x,y) and add them.
  21.     (see for yourself in 'CalculateBody')
  22.     And KaPoW. We have the fastest plasma...
  23.     (i.e. the fastest we have ever seen.(on a 486)) If I am not correct then please notify me.
  24.  
  25.     If you have any questions, comments or whips, then we would be happy to answer.
  26.  
  27.     Contact us trough E-Mail:
  28.  
  29.     fwiffo@daimi.aau.dk
  30.  
  31.             or
  32.  
  33.     martino@daimi.aau.dk
  34.  
  35.     (If you can optimize it (e.g. write it in asm) we would be very interested
  36.      to see the results!)
  37. */
  38. #include <stdio.h>
  39. #include <stdlib.h>
  40. #include <conio.h>
  41. #include <math.h>
  42. #include <fcntl.h>
  43. #include <sys\stat.h>
  44. #include <io.h>
  45. #include <dos.h>
  46. #define uchar unsigned char
  47. #define ulong unsigned long
  48. #define uint unsigned int
  49. #define PEL_write 0x3c8
  50. #define PEL_read 0x3c7
  51. #define PEL_data 0x3c9
  52. #define box_w 160
  53. #define box_h 100
  54. #define pi 3.1415926L
  55. //#define max(a,b) ((a) > (b) ? (a) : (b))
  56. //-------------------------------------------------
  57. uchar body[box_w*box_h];    // buffer for the bitmap body
  58. uchar colors[256];            // color table
  59. uchar tab1[64000];            // table one for thr plasma
  60. uchar tab2[64000];            // table two for the plasma
  61. //-------------------------------------------------
  62.  
  63. //**********************************************************
  64. //*******************  ASM SOURCES  ************************
  65. //**********************************************************
  66.  
  67. void OnVideo(void)
  68. {
  69.     asm mov bl,0x36
  70.     asm mov ax,0x1200
  71.     asm int 0x10
  72. }
  73. void OffVideo(void)
  74. {
  75.     asm mov bl,0x36
  76.     asm mov ax,0x1201
  77.     asm int 0x10
  78. }
  79. void SetMode(char mode)
  80. {
  81.     asm mov ah,0x00
  82.     asm mov al,mode
  83.     asm int 0x10
  84. }
  85. char GetMode()
  86. {
  87.     char mode;
  88.     asm mov ah,0x0f
  89.     asm int 0x10
  90.     asm mov mode,al
  91.     return (mode);
  92. }
  93. void WaitRast()
  94. {
  95.     asm mov dx,0x3da
  96. bra1:
  97.     asm in  al,dx
  98.     asm and al,8
  99.     asm jnz bra1
  100. bra2:
  101.     asm in  al,dx
  102.     asm and al,8
  103.     asm jz bra2
  104. }
  105. void SetBank(unsigned char wbank, unsigned char rbank)
  106. {
  107.     unsigned char n=rbank<<4|wbank;
  108.     asm mov al,n
  109.     asm mov dx,0x3cd    // Point to memory segment register
  110.     asm out dx,al        // Set it.
  111. }
  112. void SetDisplayStart(int adsr)
  113. {
  114.     // This part uses standard VGA DisplayStart registers
  115.     asm mov bx,adsr        // Load start adress
  116.     asm mov dx,0x3d0    // color register (mono = 0x3b0
  117.     asm add dx,4
  118.     asm mov al,0x0d
  119.     asm out dx,al
  120.     asm inc dx
  121.     asm mov al,bl
  122.     asm out dx,al
  123.     asm dec dx
  124.     asm mov al,0x0c
  125.     asm out dx,al
  126.     asm inc dx
  127.     asm mov al,bh
  128.     asm out dx,al        // Listing 14.32
  129. }
  130.  
  131. void DubImage(int off, unsigned char buf[], int nbyte, int nrow)
  132. {
  133.     asm push es
  134.     asm mov ax,0xa000
  135.     asm mov es,ax
  136.     asm mov di,off
  137.     asm lds si,buf
  138.     asm xor dx,dx
  139.     asm mov bx,320
  140.     asm sub bx,nbyte
  141.     asm sub bx,nbyte
  142.     asm mov cx,nrow
  143.     asm xor ch,ch
  144.     asm mov ah,[bp+10]
  145.  
  146. loop1:
  147.     asm mov dx,nbyte
  148. loop2:
  149.     asm mov al,[si]
  150.     asm mov ah,al
  151.     asm mov es:[di],ax
  152.     asm inc di
  153.     asm inc di
  154.     asm inc si
  155.     asm dec dx
  156.     asm jg loop2
  157.     asm add di,bx
  158.     asm sub si,nbyte
  159.     asm mov dx,nbyte
  160. loop3:
  161.     asm mov al,[si]
  162.     asm mov ah,al
  163.     asm mov es:[di],ax
  164.     asm inc di
  165.     asm inc di
  166.     asm inc si
  167.     asm dec dx
  168.     asm jg loop3
  169.     asm add di,bx
  170.     asm loop loop1
  171.     asm pop es
  172. }
  173. void SetPal(uchar col, uchar red, uchar gre, uchar blu)
  174. {
  175.     asm mov al,col        // load palette
  176.     asm mov dx,PEL_write    // Ignite write mode
  177.     asm out dx,al        // store it
  178.     asm mov dx,PEL_data    // Initialize color data
  179.     asm mov al,red        // red color
  180.     asm out dx,al        // store it
  181.     asm mov al,gre        // green color
  182.     asm out dx,al        // store it
  183.     asm mov al,blu        // blue color
  184.     asm out dx,al        // and store it
  185. }
  186.  
  187. void CalcTab1()   // calculate table 1 for plasma
  188. {
  189.     long i=0,j=0;
  190.     while(i<box_h*2)
  191.     {
  192.         j=0;
  193.         while(j<box_w*2)
  194.         {
  195.             tab1[(i*box_w*2)+j]=(uchar) (( sqrt(16.0+(box_h-i)*(box_h-i)+(box_w-j)*(box_w-j))-4) *5 );
  196.             j++;
  197.         }
  198.         i++;
  199.     }
  200. }
  201. void CalcTab2()   // calculate table 2 for plasma
  202. {
  203.     long i=0,j=0;
  204.     float temp;
  205.     while(i<box_h*2)
  206.     {
  207.         j=0;
  208.         while(j<box_w*2)
  209.         {
  210.             temp=sqrt(16.0+(box_h-i)*(box_h-i)+(box_w-j)*(box_w-j))-4;
  211.             tab2[(i*box_w*2)+j]=(sin(temp/9.5)+1)*90;
  212. //            tab2[(i*box_w*2)+j]=(sin(sqrt((box_h-i)*(box_h-i)+(box_w-j)*(box_w-j))/9.5)+1)*90;
  213.             j++;
  214.         }
  215.         i++;
  216.     }
  217. }
  218. void CalculateColors()
  219. {
  220.     static double r=1.0/6.0*pi,g=3.0/6.0*pi,b=5.0/6.0*pi;
  221.     int i=0;
  222.     double u,v;
  223.     while(i<256)
  224.     {
  225.         u=2*pi/256*i;
  226. //#define mycol(u,a) (max(0.0,cos((u)+(a))))*63 // try this line instead
  227. #define mycol(u,a) (cos((u)+(a))+1)*31
  228.         SetPal(i,mycol(u,r),mycol(u,g),mycol(u,b));
  229.         i++;
  230.     }
  231.     r+=0.05;
  232.     g-=0.05;
  233.     b+=0.1;
  234. }
  235. void CalculateBody(long x1,long y1,long x2,long y2,long x3,long y3,long x4,long y4,long roll)
  236. {
  237.     long i=0,j=0,k=0;
  238.     char a;
  239.     while(i<box_h)
  240.     {
  241.         j=0;
  242.         k+=box_w;
  243.         while(j<box_w)
  244.         {
  245.                                 // this is the heart of the plasma
  246.             body[k+j]=
  247.                     tab1[320*(i+y1)+j+x1]+roll+
  248.                     tab2[320*(i+y2)+j+x2]+
  249.                     tab2[320*(i+y3)+j+x3]+
  250.                     tab2[320*(i+y4)+j+x4]
  251.                     ;
  252.             j++;
  253.         }
  254.         i++;
  255.     }
  256. }
  257. void DisplayObject(int x,int y)
  258. {
  259.     DubImage(x+y*320,&body[0],box_w,box_h);
  260. }
  261. int main(void)
  262. {
  263.     float circle1=0,circle2=0,circle3=0,circle4=0,circle5=0,circle6=0,circle7=0,circle8=0;
  264.     int x1,y1,x2,y2,x3,y3,x4,y4,roll=0;
  265.     int bank=0,i=0;
  266.     char oldmode;
  267.     oldmode=GetMode();
  268.     printf("\nSorry no music...\nPlease stand by, as CPU is performing heavy calculations.\n");
  269.     CalcTab1();
  270.     CalcTab2();
  271.     OnVideo();
  272.     SetMode(19);
  273.     CalculateColors();
  274.     while(!kbhit())
  275.     {
  276.         i++;
  277.         circle1+=0.085/6;
  278.         circle2-=0.1/6;
  279.         circle3+=.3/6;
  280.         circle4-=.2/6;
  281.         circle5+=.4/6;
  282.         circle6-=.15/6;
  283.         circle7+=.35/6;
  284.         circle8-=.05/6;
  285.         x2=(box_w/2)+(box_w/2)*sin(circle1);
  286.         y2=(box_h/2)+(box_h/2)*cos(circle2);
  287.         x1=(box_w/2)+(box_w/2)*cos(circle3);
  288.         y1=(box_h/2)+(box_h/2)*sin(circle4);
  289.         x3=(box_w/2)+(box_w/2)*cos(circle5);
  290.         y3=(box_h/2)+(box_h/2)*sin(circle6);
  291.         x4=(box_w/2)+(box_w/2)*cos(circle7);
  292.         y4=(box_h/2)+(box_h/2)*sin(circle8);
  293.         CalculateBody(x1,y1,x2,y2,x3,y3,x4,y4,roll+=5);
  294.         DisplayObject((320-box_w*2)/2,0);
  295. //        WaitRast();
  296.         if (bank==0)
  297.         {
  298.             bank=1;
  299.             SetBank(0,0);
  300.             SetDisplayStart(16384);
  301.             CalculateColors();
  302.         }
  303.         else
  304.         {
  305.             bank=0;
  306.             SetBank(1,1);
  307.             SetDisplayStart(0);
  308.         }
  309.     }
  310.     OffVideo();
  311.     SetMode(oldmode);
  312.     return 0;
  313. }
  314.